﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Data_Object;
using Models;

namespace Business_Object
{
    public class XmlParser
    {
        private IDichVuDao _service = DataAccess.DichVuDao;
        public XmlDocument ConvertObjectToXml(List<DiaDiem> diaDiems )
        {
            XmlDocument doc = new XmlDocument();

            XmlElement root = doc.CreateElement("markers");

            foreach (DiaDiem diaDiem in diaDiems)
            {
                XmlElement marker = doc.CreateElement("marker");

                XmlAttribute id = doc.CreateAttribute("id");
                id.Value = diaDiem.MaDiaDiem.ToString();
                marker.SetAttributeNode(id);            
                XmlAttribute name = doc.CreateAttribute("name");
                name.Value = diaDiem.TenDiaDiem;
                marker.SetAttributeNode(name);
                XmlAttribute description = doc.CreateAttribute("description");
                description.Value = diaDiem.MieuTa;
                marker.SetAttributeNode(description);
                XmlAttribute address = doc.CreateAttribute("address");
                address.Value = diaDiem.DiaChi;
                marker.SetAttributeNode(address);
                XmlAttribute country = doc.CreateAttribute("country");
                country.Value = diaDiem.QuocGia;
                marker.SetAttributeNode(country);
                XmlAttribute lat = doc.CreateAttribute("lat");
                lat.Value = diaDiem.ViDo.ToString();
                marker.SetAttributeNode(lat);
                XmlAttribute lng = doc.CreateAttribute("lng");
                lng.Value = diaDiem.KinhDo.ToString();
                marker.SetAttributeNode(lng);
                XmlAttribute type = doc.CreateAttribute("type");
                DichVu dichVu = _service.Find(diaDiem.MaDichVu);
                type.Value = dichVu.TenDichVu;
                marker.SetAttributeNode(type);

                root.AppendChild(marker);

            }

            doc.AppendChild(root);
            return doc;

        }

        public XmlDocument ConvertObjectToXml(List<DichVu> dichVus)
        {

            XmlDocument doc = new XmlDocument();

            XmlElement root = doc.CreateElement("services");

            foreach (DichVu dichVu in dichVus)
            {
                XmlElement service = doc.CreateElement("service");

                XmlAttribute value = doc.CreateAttribute("value");
                value.Value = dichVu.MaDichVu.ToString();
                service.SetAttributeNode(value);

                XmlAttribute name = doc.CreateAttribute("name");
                name.Value = dichVu.TenDichVu;
                service.SetAttributeNode(name);

                root.AppendChild(service);

            }

            doc.AppendChild(root);
            return doc;
        }
    }
}
